#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<stack>
using namespace std;


class Solution {
public:
    bool validateStackSequences(vector<int>& pushed, vector<int>& popped) {
        if(pushed.empty()&&popped.empty()){
            return true;
        }
        stack<int> S;
        int i = 0;
        for(int num:pushed){
            S.push(num);
            while(!S.empty()&&S.top()==popped[i]){
                S.pop();
                ++i;
            }
        }
        
        return S.empty();
    }  
};